package com.wangnan.currentactivity.Dao;

import android.content.Context;
import android.os.AsyncTask;

import java.util.List;

import androidx.lifecycle.LiveData;

/*
 * Package    :com.wangnan.currentactivity.Dao
 * ClassName  :BillListRepository
 * Description:
 * Data       :2021/1/31/031 14:10
 * 数据仓库的意思
 */
public class BillListRepository {
	LiveData<List<BillList>> allbillListsLive;
	BillListDao billListDao;
	public BillListRepository(Context context) {
	     BillListDatabase billListDatabase = BillListDatabase.getDatabase(context.getApplicationContext());
		 billListDao = billListDatabase.getBillListDao();
		allbillListsLive = billListDao.getAllBillLists();
	}

	public void insertBillLists(BillList... billLists) {
		new InsertAsyncTask(billListDao).execute(billLists);
	}

	public void updateBillLists(BillList... billLists) {
		new UpdateAsyncTask(billListDao).execute(billLists);
	}

	public void deleteBillLists(BillList... billLists) {
		new DelectAsyncTask(billListDao).execute(billLists);
	}

	public void deleteAllBillLists(BillList... billLists) {
		new DelectAllAsyncTask(billListDao).execute();
	}

	public LiveData<List<BillList>> getAllbillListsLive() {
		return allbillListsLive;
	}


	static class InsertAsyncTask extends AsyncTask<BillList, Void, Void> {     //0 BillList: 参数   1Void :进度进展   2Void:  结果
		private BillListDao billListDao;

		public InsertAsyncTask(BillListDao billListDao) {
			this.billListDao = billListDao;
		}

		@Override
		protected Void doInBackground(BillList... billLists) {
			billListDao.insertBillLists(billLists);
			return null;
		}
	}
	static class UpdateAsyncTask extends AsyncTask<BillList, Void, Void> {     //0 BillList: 参数   1Void :进度进展   2Void:  结果
		private BillListDao billListDao;

		public UpdateAsyncTask(BillListDao billListDao) {
			this.billListDao = billListDao;
		}

		@Override
		protected Void doInBackground(BillList... billLists) {
			billListDao.updateBillLists(billLists);
			return null;
		}
	}

	static class DelectAsyncTask extends AsyncTask<BillList, Void, Void> {     //0 BillList: 参数   1Void :进度进展   2Void:  结果
		private BillListDao billListDao;

		public DelectAsyncTask(BillListDao billListDao) {
			this.billListDao = billListDao;
		}

		@Override
		protected Void doInBackground(BillList... billLists) {
			billListDao.deleteAllBillLists();
			return null;
		}
	}

	static class DelectAllAsyncTask extends AsyncTask<Void, Void, Void> {     //0 BillList: 参数   1Void :进度进展   2Void:  结果
		private BillListDao billListDao;

		public DelectAllAsyncTask(BillListDao billListDao) {
			this.billListDao = billListDao;
		}

		@Override
		protected Void doInBackground(Void... voids) {
			billListDao.deleteAllBillLists();
			return null;
		}
	}
}